//
//  WQCLoginViewController.swift
//  3-微博
//
//  Created by DDDD on 16/2/26.
//  Copyright © 2016年 gongsimingzi. All rights reserved.
//

import UIKit
import SnapKit

//声明代理

protocol WQCVisitorDelegate :NSObjectProtocol{

    func didLogin()
    
    func didRequired()
}


class WQCVisitor: UIView {
    
    //设置代理属性
    
   weak var delegate : WQCVisitorDelegate?
    
    //重写 init 方法
    override init(frame :CGRect){
        super.init(frame:frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //
    func setupUI(){
        addSubview(homeIconImageView)
         addSubview(maskImageView)
        addSubview(iconImageView)
        
        

        addSubview(messageLebal)
        addSubview(registerButton)
        addSubview(loginButton)
       
        
        //中间图片的约束
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //旋转图片的约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //文字介绍的约束
        messageLebal.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLebal.snp_left)
            
            make.top.equalTo(messageLebal.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLebal.snp_right)
            
            make.top.equalTo(messageLebal.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        
    }
    
    //
    func login(){
    
//        print("visitor 登陆")
        delegate?.didLogin()
        
    }
    
    func register(){
//         print("visitor 注册")
        delegate?.didRequired()
    }
    //
    func setupVisitor(message: String, imageName : String ,Animation: Bool){
        
        if Animation {
            
            startAnimaion()
            
            homeIconImageView.hidden = false
            
        }else{
            homeIconImageView.hidden = true
        }
        
        messageLebal.text = message
        
        iconImageView.image = UIImage(named: imageName)
        
    }
    //添加动画
    func startAnimaion(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //动画转动范围
        animation.toValue = 2 * M_2_PI
        //动画转动次数
        animation.repeatCount = MAXFLOAT
        //动画转动时间
        animation.duration = 5
        //切换视图时动画不消失
        animation.removedOnCompletion = false
        
        homeIconImageView.layer.addAnimation(animation, forKey: nil )
    }
    //懒加载
    /// 首页动画图片
    private lazy var homeIconImageView :UIImageView = {
       let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    return img
        }()
    /// 通用图片
    private lazy var iconImageView :UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return img
        
        }()
    /**
    *   消息文字
    */
    private lazy var messageLebal :UILabel = {
        let lebal = UILabel()

        lebal.font = UIFont.systemFontOfSize(14)
        
        lebal.text = "关注一些人，回这里看看有什么惊喜"
        
        lebal.textColor = UIColor.darkGrayColor()

        lebal.textAlignment = .Center
        
        lebal.numberOfLines = 0
        
        return lebal
        }()
    //注册按钮
    private lazy var registerButton : UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    //登陆按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
    button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    private lazy var maskImageView : UIImageView = {
            let img = UIImageView()
        
            img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
            return img
    }()
    
}